package com.yoi.Utils;


import com.yoi.bean.Constants;
import lombok.Data;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class MyRabbitMQConfig {

    @Data
    @Configuration
    @ConfigurationProperties(prefix = "spring.rabbitmq.test")
    public class RabbitConfigTest {
        private String host;
        private int port;
        private String username;
        private String password;
        private String virtualhost;
    }

    @Data
    @Configuration
    @ConfigurationProperties(prefix = "spring.rabbitmq.test2")
    public class RabbitmqConfigTest2{
        private String host;
        private int port;
        private String username;
        private String password;
        private String virtualhost;
    }

    //发送到交换器后接收ACK回调
    final RabbitTemplate.ConfirmCallback confirmCallback = new RabbitTemplate.ConfirmCallback() {
        @Override
        public void confirm(CorrelationData correlationData, boolean ack, String cause) {
            System.out.println("correlationDate:" + correlationData);
            String messageId = correlationData.getId();
            if (ack) {
                //brokerMessageLogMapper.changeBrokerMessageLogStatus(messageId,Constants.ORDER_SEND_SUCCESS);

            } else {
                System.out.println("异常处理");
            }
        }
    };
    //无相应队列与交换机绑定时的回调
    final RabbitTemplate.ReturnCallback returnCallback = new RabbitTemplate.ReturnCallback() {
        @Override
        public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {

        }
    };

    @Bean(name = "CustomerConnectionFactory")

    public ConnectionFactory FirstConnectionFactory(RabbitConfigTest Recv) {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(Recv.host);
        connectionFactory.setPort(Recv.port);
        connectionFactory.setUsername(Recv.username);
        connectionFactory.setPassword(Recv.password);
        connectionFactory.setVirtualHost(Recv.virtualhost);
        return connectionFactory;
    }

    @Bean(name="Customer2ConnectionFactory")
    @Primary
    public ConnectionFactory SecondConnectionFactory(RabbitmqConfigTest2 Recv2){
        CachingConnectionFactory connectionFactory=new CachingConnectionFactory();
        connectionFactory.setHost(Recv2.host);
        connectionFactory.setPort(Recv2.port);
        connectionFactory.setUsername(Recv2.username);
        connectionFactory.setPassword(Recv2.password);
        connectionFactory.setVirtualHost(Recv2.virtualhost);
        return connectionFactory;
    }

    @Bean(name = "CustomerRabbitTemplate")

    public RabbitTemplate RabbitTemplate(
            @Qualifier("CustomerConnectionFactory") ConnectionFactory connectionFactory
    ) {
        //使用外部事物
        //lpzRabbitTemplate.setChannelTransacted(true);
        return new RabbitTemplate(connectionFactory);
    }

    @Bean(name = "Customer2RabbitTemplate")
    @Primary
    public RabbitTemplate RabbitTemplate2(
            @Qualifier("Customer2ConnectionFactory") ConnectionFactory connectionFactory
    ) {
        //使用外部事物
        //lpzRabbitTemplate.setChannelTransacted(true);
        return new RabbitTemplate(connectionFactory);
    }

    @Bean(name = "CustomerContainerFactory")
    public SimpleRabbitListenerContainerFactory CustomerContainerFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            @Qualifier("CustomerConnectionFactory") ConnectionFactory connectionFactory
    ) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    }

    @Bean(name = "Customer2ContainerFactory")
    public SimpleRabbitListenerContainerFactory Customer2ContainerFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            @Qualifier("Customer2ConnectionFactory") ConnectionFactory connectionFactory
    ) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    }
}